﻿using UnityEngine;
using QFramework;

namespace HareGame
{
    public partial class Util
    {
        /// <summary>
        /// 世界坐标转换为屏幕坐标
        /// </summary>
        /// <param name="worldPoint">屏幕坐标</param>
        /// <returns></returns>
        public Vector2 WorldPointToScreenPoint(Vector3 worldPoint)
        {
            // Camera.main 世界摄像机
            Vector2 screenPoint = Camera.main.WorldToScreenPoint(worldPoint);
            return screenPoint;
        }

        /// <summary>
        /// 屏幕坐标转换为世界坐标
        /// </summary>
        /// <param name="screenPoint">屏幕坐标</param>
        /// <param name="planeZ">距离摄像机 Z 平面的距离</param>
        /// <returns></returns>
        public Vector3 ScreenPointToWorldPoint(Vector2 screenPoint, float planeZ)
        {
            // Camera.main 世界摄像机
            Vector3 position = new Vector3(screenPoint.x, screenPoint.y, planeZ);
            Vector3 worldPoint = Camera.main.ScreenToWorldPoint(position);
            return worldPoint;
        }

        // RectTransformUtility.WorldToScreenPoint
        // RectTransformUtility.ScreenPointToWorldPointInRectangle
        // RectTransformUtility.ScreenPointToLocalPointInRectangle
        // 上面三个坐标转换的方法使用 Camera 的地方
        // 当 Canvas renderMode 为 RenderMode.ScreenSpaceCamera、RenderMode.WorldSpace 时 传递参数 canvas.worldCamera
        // 当 Canvas renderMode 为 RenderMode.ScreenSpaceOverlay 时 传递参数 null

        /// <summary>
        /// UI坐标转换为屏幕坐标
        /// </summary>
        /// <param name="uiPoint">ui坐标</param>
        /// <returns></returns>
        public Vector2 UIPointToScreenPoint(Vector3 uiPoint)
        {
            // RectTransform：target
            // worldPoint = target.position;
            Camera uiCamera = GameEntry.UI.GetUICamera();

            Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(uiCamera, uiPoint);
            return screenPoint;
        }

        /// <summary>
        /// 世界坐标转UGUI坐标
        /// </summary>
        /// <param name="worldPoint">世界坐标</param>
        /// <param name="rt">ui的RectTransform</param>
        /// <returns></returns>
        public Vector2 WorldPointToUIPoint(Vector3 worldPoint, RectTransform rt)
        {
            Vector2 screenPoint = WorldPointToScreenPoint(worldPoint);
            return ScreenPointToUIPoint(rt, screenPoint);
        }

        /// <summary>
        /// 屏幕坐标转UGUI坐标
        /// </summary>
        /// <param name="rt">ui的RectTransform</param>
        /// <param name="screenPoint">屏幕坐标</param>
        /// <returns></returns>
        public Vector3 ScreenPointToUIPoint(RectTransform rt, Vector2 screenPoint)
        {
            Vector3 globalMousePos;
            //UI屏幕坐标转换为世界坐标
            Camera uiCamera = GameEntry.UI.GetUICamera();

            // 当 Canvas renderMode 为 RenderMode.ScreenSpaceCamera、RenderMode.WorldSpace 时 uiCamera 不能为空
            // 当 Canvas renderMode 为 RenderMode.ScreenSpaceOverlay 时 uiCamera 可以为空
            RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, screenPoint, uiCamera, out globalMousePos);
            // 转换后的 globalMousePos 使用下面方法赋值
            // target 为需要使用的 UI RectTransform
            // rt 可以是 target.GetComponent<RectTransform>(), 也可以是 target.parent.GetComponent<RectTransform>()
            // target.transform.position = globalMousePos;
            return globalMousePos;
        }

        /// <summary>
        /// 屏幕坐标转换为UGUI的anchoredPosition
        /// </summary>
        /// <param name="parentRT">ui的RectTransform</param>
        /// <param name="screenPoint">屏幕坐标</param>
        /// <returns></returns>
        public Vector2 ScreenPointToUILocalPoint(RectTransform parentRT, Vector2 screenPoint)
        {
            Vector2 localPos;
            Camera uiCamera = GameEntry.UI.GetUICamera();

            RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRT, screenPoint, uiCamera, out localPos);
            // 转换后的 localPos 使用下面方法赋值
            // target 为需要使用的 UI RectTransform
            // parentRT 是 target.parent.GetComponent<RectTransform>()
            // 最后赋值 target.anchoredPosition = localPos;
            return localPos;
        }

    }
}
